package com.chefu.common.util;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/8/11 0011.
 */
public class ToastUtils {

    /**
     * 短时间显示toast
     * @param context
     * @param message
     */
    public static void showMessage(@NonNull Context context, @Nullable String message){

        show(context,message,0, Toast.LENGTH_SHORT);
    }
    /**
     * 短时间显示toast
     * @param context
     */
    public static void showMessage(@NonNull Context context, @IdRes int id){

        show(context,null,id, Toast.LENGTH_SHORT);
    }

    /**
     * 长时间显示toast
     * @param context
     * @param message
     */
    public static void showMessageLong(@NonNull Context context, @Nullable String message){

        show(context,message,0, Toast.LENGTH_LONG);
    }

    private static Toast mToast;

    /***
     * toast 显示 可在子线程中使用 避免多次点击 toast 不消失问题
     * @param context
     * @param message
     * @param time
     */
    private static void show(Context context, final String message, final int resId, final int time){

        if (context == null)
            return;

            if (mToast == null) {

                try {

                final Activity activity = (Activity) context;

                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if (message == null || "".equals(message)){
                            mToast = Toast.makeText(activity,activity.getResources().getString(resId)+"",time);
                        }else{
                            mToast = Toast.makeText(activity,message+"",time);
                        }

                    }
                });

                } catch (Exception e) {

                    if (message == null || "".equals(message)){
                        mToast = Toast.makeText(context,context.getResources().getString(resId)+"",time);
                    }else{
                        mToast = Toast.makeText(context,message+"",time);
                    }

                }

            }else{
                mToast.setText(message+"");
            }

        mToast.show();

    }


}
